import config from '@/config/config.js';
import http from '@/api/http.js'

let materialRequest={
    loadMaterials(material,page=1,limit=10,callBack){
        // 获取token
        let token = localStorage.getItem("token");
        if(token){
            let url = config.mdBaseUrl + "/material/list?page="+page + "&limit=" + limit;
            if(material){
                if (material.classify){
                    url += "&classify=" + material.classify;
                }
                if(material.materialCode){
                    url += "&materialCode=" + material.materialCode;
                }
                if(material.materialName){
                    url += "&materialName=" + material.materialName;
                }
            }
            http.get(url,callBack);
        }else{
            callBack(undefined);
        }
    }, save(material,callBack){
        // 获取token
        let token = localStorage.getItem("token");
        if(token){
            let url = config.mdBaseUrl + "/material/save";
            http.post(url,material,callBack);
        }else{
            callBack(undefined);
        }
    }, updateMaterialById(material,callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/material/update";
            http.put(url,material,callBack);
        } else {
            callBack(undefined);
        }
    }, selectById(materialId,callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/material/selectById?materialId=" + materialId;
            http.get(url,callBack);
        } else {
            callBack(undefined);
        }
    }, deleteMaterialById(materialId,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/material/delete?materialId=" + materialId;
            http.delete(url,callBack);
        } else {
            callBack(undefined);
        }
    }, deletesMaterial(materialIds,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.mdBaseUrl + "/material/deletes?a=1";
            if(materialIds){
                materialIds.forEach(id=> url += '&materialId='+id);
            }
            http.delete(url,callBack);
        } else {
            callBack(undefined);
        }
    },getAllMaterial(callBack){
        // 获取token
        let token = localStorage.getItem("token");
        if(token){
            let url = config.mdBaseUrl + "/material/getAllMaterial";
            http.get(url,callBack);
        }else{
            callBack(undefined);
        }
    }

}

export default materialRequest;